package server;

import java.io.*;
import java.net.*;

public class SendThread extends Thread{
		
	Socket connectionSocket ;
	BufferedReader inFromClient ;	
	DataOutputStream outToClient ;
				
	public SendThread(Socket connectionSocket_var) throws IOException{
		System.out.println("\nnew SendThread for host" + connectionSocket_var.getInetAddress());
		
		connectionSocket = connectionSocket_var;		
		inFromClient = 
			new BufferedReader(new 
					InputStreamReader(connectionSocket.getInputStream()));
		outToClient = 
			new DataOutputStream(connectionSocket.getOutputStream());			
	}
	
	public void run(){
		int i = 0;		
		try{				
			while(true){													
																	
				String msgToClient = "Server's thread "+ this.getId() +" is in loop_" + i + '\n';
				outToClient.writeBytes(msgToClient);				
				
				if(i > 10000){
					i = 0; //reset i
				}
				i++;
					
				Thread.sleep(400);
			}					
		
		}
		catch (IOException e) { 
//			System.out.println("IOException: " + e);
			System.err.println("Host " + connectionSocket.getInetAddress() + " disconnected !");
			this.interrupt();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
		}
	}

}
